// src/stores/counterStore.ts
import { defineStore } from "pinia";
import { ref, onMounted, watch } from "vue";
import { useConWallet } from "@/hooks/concactWallet";
// 入口检测
import { Register, Login } from "@/api/modules/Common";
// 用户信息
import {
  Info,
  InvitedCode,
  Config,
  GoodsThumb,
  AccountRecord,
} from "@/api/modules/UserInfo";
import { showSuccessToast, showFailToast, showToast } from "vant";
import type { UserInfo, Config, TransactionRecord } from "@/types";
export const useInfoStore = defineStore("InfoStore", () => {
  // 获取当前账号
  const account = useConWallet().account;

  // 刷新我的明细的状态
  const refreshDetails = ref(false);
  // 等待登录成功了才能去请求
  const stateLogin = ref(false);
  // 获取入口检测
  const getLogin = async (address) => {
    try {
      const res = await Login({
        address,
      });
      if (res.code === 0) {
        sessionStorage.setItem("token", res.data._token);
        getInfo();
        getInvitedCode();
        // getConfig();
        // getGoodsThumb();
        stateLogin.value = true;
        console.log("====================================");
        console.log("登录成功", stateLogin.value);
        console.log("====================================");
      } else {
        getInviteCode(address);
      }
      console.log(res);
    } catch (err) {
      console.log(err);
    }
  };
  // 用邀请码去注册
  // 获取邀请码
  const showInvterCode = ref<boolean>(false);
  const inviteCode = ref<string>("");
  const getInviteCode = (address) => {
    const match = window.location.href.match(/code=(\d+)/);
    const code = match ? match[1] : null;
    inviteCode.value = code;

    if (code) {
      const codeSlice = code.slice(0, 6);
      getRegister(codeSlice, address);
    } else {
      showInvterCode.value = true;
    }
  };
  // 判断有没有邀请码，没有就先
  const getRegister = async (code, address) => {
    try {
      const res = await Register({
        invite_code: code,
        address,
      });
      if (res.code === 0) {
        sessionStorage.setItem("token", res.data._token);
        getInfo();
        getInvitedCode();
        // getConfig();
        // getGoodsThumb();
        showToast(res.msg);
        showInvterCode.value = false;
      } else {
        showToast(res.msg);
        showInvterCode.value = true;
      }
      console.log(res);
    } catch (err) {
      console.log(err);
    }
  };
  // 获取用户信息
  const infomation = ref<UserInfo>({
    user_id: 0, // 用户ID
    address: "", // 用户地址
    parent_id: 0, // 上级ID
    usdt: "", // USDT币
    dayou: "", // Dayou币
    compute_power: "", // 算力
    total_commission: 0, // 累积佣金
    parentAddress: "0000", //父级地址
  });
  const config = ref<Config>({
    usdt_to_power: "", // 1 USDT = x 算力
    buy_min_power: "", // 起购算力
  });
  // 自动复投
  const checked = ref<boolean>(false);
  const getInfo = async () => {
    try {
      const res = await Info();
      if (res.code === 0) {
        Object.assign(infomation.value, res.data.userinfo);
        Object.assign(config.value, res.data.config);
        stateLogin.value = true;
      }
    } catch (err) {
      console.log(err);
    }
  };
  // 邀请链接
  const link = ref<string>("");
  const getInvitedCode = async () => {
    try {
      const res = await InvitedCode();
      if (res.code === 0) {
        link.value = res.data.invite_url;
      }
    } catch (error) {
      console.log("Error getting invited code:", error);
    }
  };
  //账户流水
  const getAccountRecord = async () => {
    try {
      const res = await AccountRecord({
        page: "1",
        currency: "1",
      });
    } catch (error) {
      console.log(error);
    }
  };
  return {
    getLogin,
    getRegister,
    infomation,
    config,
    link,
    getInfo,
    showInvterCode,
    checked,
    getAccountRecord,
    inviteCode,
    refreshDetails,
    stateLogin,
  };
});
